<?php
/**
*
*	配置生成函数库 @zairwolf
*/

if(!defined('IN')) {
	exit('Access Denied (config.func.php)');
}


function _config($cacheName, $prefix = 'config_'){
	$f = ROOT.'data/cache/'.$prefix.$cacheName.'.php';

	if(!is_file($f))
		_config_update($cacheName, $prefix);

	return $f;
}

function _config_update($cacheName = '', $prefix = 'config_'){
	if($cacheName) {
		$caches = array($cacheName);
	}else {
		$caches = array('system', 'user', 'display', 'read', 'upload', 'fopen', 'seo', 'safe', 'info', 'rank');
	}

	foreach($caches as $cacheName) {
		$f = ROOT.'data/cache/'.$prefix.$cacheName.'.php';

		$cacheData = _config_generate($cacheName);

		rfow($f, '<'."?php\nif(!defined('IN')) exit('Access Denied');\n".$cacheData);
	}
}

function _config_generate($cacheName){
	global $db;

	$data = db_rows("select title,info from rd8_system where config='".strtoupper($cacheName)."'");

	$cacheData = array();
	foreach($data as $v)
		$cacheData[$v['title']] = $v['info'];

	switch($cacheName) {
		case 'info':
			$ks = array(
				'about',
				'guide',
				'contact',
				'pay',
				'fee',
				'novel',
				'author',
				'eula',
				'regemail',
				'regactiveintro',
				'help',
				'copyright',
				'link',
				'invite',
				'around',
				'userrule',
				'visitorview',
				'visitordownload',
				'forgetshow',
				'forgetemail',
			);
			$html = $cacheData['html'];
			unset($cacheData['html']);
			foreach(explode(',', $html) as $k => $v) {
				$cacheData['html'][$ks[$k]] = $v;
			}
			break;
	}

	return '$_SYSTEM[\''.strtoupper($cacheName).'\'] = '.var_export($cacheData, true).';';
}
